<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class FenxiaoController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Fenxiao");

		$this->log= D("GuanLog");
		$this->fenzhang= D("OrderFenzhang");

	}
	
	function index(){




		$categorys=$this->db->select();

		$this->assign("list",$categorys);


		$this->display();
	}

	/**
	 *  添加分类
	 */
	public function add() {
		if (IS_POST) {





			if ($this->db->create()) {
				if ($id=$this->db->add()!==false) {




					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{

			$admins=D("Admin")->select();
			$this->assign("admins",$admins);

		$this->display("form");
		}
	}

	function edit(){




		if (IS_POST) {
			if ($this->db->create()) {
				if ($this->db->save()!==false) {
					$this->success("保存成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}
		else{
		$id= intval(I("get.id"));
		$fenxiao=$this->db->where("id=$id")->find();

			$admins=D("Admin")->select();
			$this->assign("admins",$admins);



		$this->assign("data",$fenxiao);
		$this->assign("edit",true);
		$this->display("form");
		}
	}
	

	function delete(){
	
			$id = intval(I("get.id"));
			
			if(!$id){
				$this->error("缺少参数！");
			}


			if ($this->db->delete($id)) {


				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}

	}
	








	public function  code(){


		$id=I("get.id");

		$ticket=$this->db->where(array("id"=>$id))->getField("ticket");

		if(!$ticket){

			$code=$this->wk->qrcodeCreate($id);

		    $ticket=$code["ticket"];
			$data=array(
				"id"=>$id,
				"ticket"=>$code["ticket"]
			);
			$this->db->save($data);



		}



		$code=$this->wk->showqrcode($ticket);

		$this->assign("code",$code);

		$this->display("");




	}


	public function  log(){


		$search=false;


		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));

		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}

		$map['j_id']  =I("get.id");;

//		$tel=I("get.tel");
//
//		if($tel){
//
//			$j_id=$this->db->where(array("tel"=>$tel))->getField("id");
//			$map['j_id']  =$j_id;
//			$search=true;
//
//
//
//		}

		$this->assign("search",$search);

		$count=$this->log->where($map)->count();
		$page = $this->page($count);
		$list=$this->log->limit($page->firstRow . ',' . $page->listRows)->order ( "inputtime desc")->where($map)->select();



		$this->assign("Page",$page->show(""));







		$this->assign("list",$list);
		$this->display();







	}

	public  function  result(){

		$search=false;


		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));


		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}




		$map['j_id']  =I("get.j_id");



		$this->assign("search",$search);


		$sum = $this->fenzhang->where($map)->sum('money');
		$fen_sum = $this->fenzhang->where($map)->sum('fen_money');

		$count=$this->fenzhang->where($map)->count();
		$page = $this->page($count);
		$list=$this->fenzhang->limit($page->firstRow . ',' . $page->listRows)->order ( "inputtime desc")->where($map)->select();

		$this->assign("Page",$page->show(""));



		$this->assign("list",$list);
		$this->assign("count",$count);
		$this->assign("sum",$sum);
		$this->assign("fen_sum",$fen_sum);







		$this->display();





	}



	public function  baobiao(){

		$search=false;

		$type=I("get.type")?I("get.type"):"tongji";

		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));


		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}






		$this->assign("search",$search);



		if($search){



			if($type=="tongji"){

				$list=$this->fenzhang->field("j_id,sum(money) as sum ,sum(fen_money) as sum_fen ,count(order_sn) as nums")->order ( "inputtime desc")->where($map)->group("j_id")->select();



				$this->assign("list",$list);

				$this->display();

			}
			if($type=="xiangxi"){


				$list=$this->fenzhang->order ( "inputtime desc")->where($map)->select();

				$this->assign("list",$list);


				$this->display("xiangxi");

			}







		}
		else{

			$this->display();
		}






	}





	public function  daochu(){

		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));




		if(!$starttime){
			$this->error("请填写开始时间");

		}


		if(!$starttime){
			$this->error("请填写结束时间");
		}

		if($starttime>$endtime){
			$this->error("开始时间不能大于结束时间");



		}



		$map['inputtime']  = array('between',array($starttime,$endtime));

		$tongji=$this->fenzhang->field("j_id,sum(money) as sum ,sum(fen_money) as sum_fen ,count(order_sn) as nums")->order ( "inputtime desc")->where($map)->group("j_id")->select();



		$xiangxi=$this->fenzhang->order ( "inputtime desc")->where($map)->select();

		import("Org.Util.PHPExcel");
		import("Org.Util.PHPExcel.Reader.Excel5");
		import("Org.Util.PHPExcel.Writer.Excel5");
		import("Org.Util.PHPExcel.IOFactory.php");



		$filename=EXCELS."/tongji.xls";


		$objPHPExcel=new \PHPExcel();
		$objPHPExcel=new \PHPExcel_Reader_Excel5();
		//载入文件

		$objPHPExcel=$objPHPExcel->load($filename);

		$objActSheet = $objPHPExcel->setActiveSheetIndex(0);

		//填入统计选项标题

		$column = 4;

		//填入标题
		$objActSheet->setCellValue('A1',"分销统计表");
		$objActSheet->setCellValue('A2',I('get.starttime')."----".I('get.endtime'));


		$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20.25);


		//写入主体数据
		$s=0;
		foreach ($tongji as $k=>$v){
			$s++;;

			$objActSheet->setCellValue('A'.$column, F("Fenxiao")[$v['j_id']]["name"]);
			$objActSheet->setCellValueExplicit('B'.$column, F("Fenxiao")[$v['j_id']]["tel"],\PHPExcel_Cell_DataType::TYPE_STRING);

			$objActSheet->setCellValue('C'.$column, F("Fenxiao")[$v['j_id']]["alipay_mail"]);
			$objActSheet->setCellValue('D'.$column, F("Fenxiao")[$v['j_id']]["bank"]);
			$objActSheet->setCellValue('E'.$column, F("Fenxiao")[$v['j_id']]["bank_card"]);
			$objActSheet->setCellValue('F'.$column, $v["nums"]);
			$objActSheet->setCellValue('G'.$column, $v["sum"]);

			$objActSheet->setCellValue('H'.$column, $v["sum_fen"]);




			$column++;

		}


		$objActSheet = $objPHPExcel->setActiveSheetIndex(1);

		//填入统计选项标题

		$column = 4;

		//填入标题
		$objActSheet->setCellValue('A1',"分销明细表");
		$objActSheet->setCellValue('A2',I('get.starttime')."----".I('get.endtime'));




		//写入主体数据
		$s=0;
		foreach ($xiangxi as $k=>$v){
			$s++;;
			$objActSheet->setCellValueExplicit('A'.$column, $v["order_sn"],\PHPExcel_Cell_DataType::TYPE_STRING);


			if($v[modal]=="product"){
				$objActSheet->setCellValue('B'.$column, "保养");
			}
			if($v[modal]=="chongzhi"){

				$objActSheet->setCellValue('B'.$column, "充值");
			}

			$objActSheet->setCellValue('C'.$column, F("Fenxiao")[$v['j_id']]["name"]);

			$objActSheet->setCellValue('D'.$column, $v["uid"]);

			$objActSheet->setCellValueExplicit('E'.$column, get_username($v["uid"]),\PHPExcel_Cell_DataType::TYPE_STRING);
			$objActSheet->setCellValue('F'.$column, date("Y-m-d H:i:s",$v["inputtime"]));

			$objActSheet->setCellValue('G'.$column, $v["money"]);
			$objActSheet->setCellValue('H'.$column, F("Fenxiao")[$v['j_id']]["bili"]);
			$objActSheet->setCellValue('I'.$column, $v["fen_money"]);







			$column++;

		}
		$objActSheet = $objPHPExcel->setActiveSheetIndex(0);

		$filename=iconv('utf-8', 'gb2312', I('get.starttime')."-".I('get.endtime')."分销明细表.xls");
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;filename=".$filename);
		header('Cache-Control: max-age=0');

		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output'); //文件通过浏览器下载
		exit;



	}




}